package com.xdtech.hotel.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.common.utils.HibernateHelper;
import com.xdtech.hotel.model.Device;
import com.xdtech.hotel.monitor.ClientMonitor;
import com.xdtech.hotel.protocol.manager.DeviceMonitor;
import com.xdtech.hotel.protocol.manager.OperationManager;
import com.xdtech.hotel.protocol.vo.RCUDevice;
import com.xdtech.hotel.service.DeviceService;
import com.xdtech.hotel.util.HotelConstants;
import com.xdtech.hotel.vo.DeviceItem;
import com.xdtech.web.model.ComboBox;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:06:39
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class DeviceController {
	@Autowired
	private DeviceService deviceService;
	@RequestMapping(value="/device.do",params = "device")
	public ModelAndView skipDevice() {
		return new ModelAndView("hotel/device/device_ftl");
	}
	
	@RequestMapping(value="/device.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(DeviceItem item,Pagination pg) {
		Map<String, Object> results =  deviceService.loadPageDataByConditions(pg,item,"findDeviceByCondition");
		return results;
	}
	
	
	@RequestMapping(value="/device.do",params = "editDevice")
	public ModelAndView editDevice(HttpServletRequest request,Long deviceId) {
		if (deviceId!=null) {
			request.setAttribute("deviceItem", deviceService.loadDeviceItem(deviceId));
		}
		return new ModelAndView("hotel/device/editDevice_ftl");
	}
	
	@RequestMapping(value="/device.do",params = "saveDevice")
	@ResponseBody
	public ResultMessage saveDevice(DeviceItem item) {
		ResultMessage r = new ResultMessage();
		if (deviceService.saveOrUpdateDevice(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/device.do",params = "deleteDeviceItems")
	@ResponseBody
	public ResultMessage deleteDeviceItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> deviceIds = new ArrayList<Long>();
			for (String id : tempIds) {
				deviceIds.add(Long.valueOf(id));
			}
			boolean check = deviceService.checkAllowDel(deviceIds);
			if (check) {
				deviceService.deleteDeviceInfo(deviceIds);
			}else {
				r.setSuccess(false);
				r.setMsg("设备已被客房关联无法删除，请先对客房进行解除.");
			}
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	
	
	@RequestMapping(value="/device.do",params = "loadDevices")
	@ResponseBody
	public List<ComboBox> loadDevices(String value,String text) {
		List<Device> devices = deviceService.getAll();
		List<ComboBox> comboBoxs = new ArrayList<ComboBox>();
		for (Device dv : devices)
		{
			comboBoxs.add(new ComboBox(dv.getId().toString(), dv.getName(), dv.getId().toString().equals(value)||dv.getName().equals(text)));
		}
		return comboBoxs;
	}
	
	@RequestMapping(value="/device.do",params = "monitoring")
	public ModelAndView monitoring() {
		return new ModelAndView("hotel/device/deviceMonitoring_ftl");
	}
	
	private static Map<String, String> statusNameMap = new HashMap<String, String>();
	static {
		statusNameMap.put(HotelConstants.DEV_STATUS_LINKING, "正在连接");
		statusNameMap.put(HotelConstants.DEV_STATUS_OFFLINE, "离线");
		statusNameMap.put(HotelConstants.DEV_STATUS_ONLINE, "在线");
	}
	private static Map<String, String> statusColorMap = new HashMap<String, String>();
	static {
		statusColorMap.put(HotelConstants.DEV_STATUS_LINKING, "color:orange");
		statusColorMap.put(HotelConstants.DEV_STATUS_OFFLINE, "color:red");
		statusColorMap.put(HotelConstants.DEV_STATUS_ONLINE, "color:green");
	}
	@RequestMapping(value="/device.do",params = "devMonitoring")
	@ResponseBody
	public Map<String, Object> devMonitoring(HttpServletRequest request,String clientId) {
		Map<String, Object> rs = new HashMap<String, Object>();
		JSONArray jsonArray = new JSONArray();
//		JSONObject jsonObject = new JSONObject();
//		jsonObject.put("id", "1");
//		jsonObject.put("style","color:green");
//		JSONArray dataArray = new JSONArray();
//		dataArray.add("RCU");
//		dataArray.add("202");
//		dataArray.add("name");
//		jsonObject.put("data",dataArray);
//		jsonArray.add(jsonObject);
//		
//		
//		jsonObject = new JSONObject();
//		jsonObject.put("id", "2");
//		jsonObject.put("style","color:red");
//		dataArray = new JSONArray();
//		dataArray.add("RCU2");
//		dataArray.add("203");
//		dataArray.add("name3");
//		jsonObject.put("data",dataArray);
//		jsonArray.add(jsonObject);
//		
//		jsonObject = new JSONObject();
//		jsonObject.put("id", "3");
//		jsonObject.put("style","color:orange");
//		dataArray = new JSONArray();
//		dataArray.add("RCU3");
//		dataArray.add("204");
//		dataArray.add("name4");
//		jsonObject.put("data",dataArray);
//		jsonArray.add(jsonObject);
//		
		
		List<RCUDevice> rcuDevices = ClientMonitor.loadRcuDevice(clientId);
		JSONObject jsonObject = null;
		JSONArray dataArray = null;
		for (RCUDevice rcuDevice : rcuDevices)
		{
			jsonObject = new JSONObject();
			jsonObject.put("id", rcuDevice.getId());
			jsonObject.put("style",statusColorMap.get(rcuDevice.getStatus()));
			dataArray = new JSONArray();
//			设备名称,设备序列号,设备IP,当前状态,通信端口
			dataArray.add(rcuDevice.getName());
			dataArray.add(rcuDevice.getSn());
			dataArray.add(rcuDevice.getIp());
			dataArray.add(statusNameMap.get(rcuDevice.getStatus()));
			dataArray.add(rcuDevice.getPort());
			jsonObject.put("data",dataArray);
			jsonArray.add(jsonObject);
		}
		rs.put("rows", jsonArray);
		return rs;
	}
	

}
